﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess.ViewDataSetTableAdapters;
using dhDataAcess.dataAcess.baobiaoDataSetTableAdapters ;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.produceBLL
{
   public  class productblBLL
    {
       private V_productblbaobiaoTableAdapter _V_productblbaobiaoAdapter = null;
       private V_productblbaobiaoTableAdapter V_productblbaobiaoAdapter
        {
            get
            {
                if (_V_productblbaobiaoAdapter == null) _V_productblbaobiaoAdapter = new V_productblbaobiaoTableAdapter();
                return _V_productblbaobiaoAdapter;

            }
        }
       private V_MrpTableAdapter _V_MrpAdapter = null;
       private V_MrpTableAdapter V_MrpAdapter
        {
            get
            {
                if (_V_MrpAdapter == null) _V_MrpAdapter = new V_MrpTableAdapter();
                return _V_MrpAdapter;

            }
        }
         private V_productblTableAdapter    _VproductblMasterlAdapter = null;
         private V_productblTableAdapter VproductblMasterAdapter
        {
            get
            {
                if (_VproductblMasterlAdapter == null) _VproductblMasterlAdapter = new V_productblTableAdapter();
                return _VproductblMasterlAdapter;

            }
        }
        private productblMasterTableAdapter _productblMasterlAdapter = null;
        private productblMasterTableAdapter productblMasterAdapter
        {
            get
            {
                if (_productblMasterlAdapter == null) _productblMasterlAdapter = new productblMasterTableAdapter();
                return _productblMasterlAdapter;

            }
        }
        private productblDetailTableAdapter _productblDetailAdapter = null;
        private productblDetailTableAdapter productblDetailAdapter
        {
            get
            {
                if (_productblDetailAdapter == null) _productblDetailAdapter = new productblDetailTableAdapter();
                return _productblDetailAdapter;

            }
        }
        /// <summary>
        /// 查询领料退库主表
        /// </summary>
        /// <returns></returns>

        public purchase.productblMasterDataTable getproductblMasterAll()
        {
            try
            {
                return productblMasterAdapter.GetDataproductblMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料退库从表
        /// </summary>
        /// <returns></returns>

        public purchase.productblDetailDataTable getproductblDetailAll()
        {
            try
            {
                return productblDetailAdapter.GetDataproductblDetail();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询视图
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_productblbaobiaoDataTable getV_productblbaobiaoAll()
        {
            try
            {
                return V_productblbaobiaoAdapter.GetDataV_productblbaobiaoALL ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询视图
        /// </summary>
        /// <returns></returns>

        public ViewDataSet.V_MrpDataTable getV_MrpAdapterAll()
        {
            try
            {
                return V_MrpAdapter.GetDataV_MrpALL ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询视图
        /// </summary>
        /// <returns></returns>

        public ViewDataSet.V_MrpDataTable getV_MrpAdapterAllbypname(string pname)
        {
            try
            {
                return V_MrpAdapter.GetDataV_MrpBypname (pname);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询视图
        /// </summary>
        /// <returns></returns>

        public ViewDataSet.V_MrpDataTable getV_MrpAdapterAllbylikepname(string pname)
        {
            try
            {
                return V_MrpAdapter.GetDataV_MrpBylikepname (pname);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询领料退库表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.productblMasterDataTable getproductblMasterAllBybillNo(string billNo)
        {
            try
            {
                return productblMasterAdapter.GetDataproductblMasterBybillNo(billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据备料单号查询备料表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public ViewDataSet.V_productblDataTable  getV_productblMasterAllBybillNo(string billNo)
        {
            try
            {
                return VproductblMasterAdapter.GetDataV_productblBybilloNo (billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询领料退库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.productblDetailDataTable getsproductblDetailAllBybillNo(string billNo)
        {
            try
            {
                return productblDetailAdapter.GetDataproductblDetaiBybillo (billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 修改领料退库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productblInsert(productblMasterModel row, purchase.productblDetailDataTable pTable, string state,string user, string billNo, string Date)
        {
            productblDetailModel productblDetailRow = new productblDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productblMasterDAL productblMaster = new productblMasterDAL(tn, conn);
                productblDetailDAL productblDetail = new productblDetailDAL(tn, conn);
                basListDAL basList = new basListDAL(tn, conn);
                if (state == "新增")
                {

                    purchase.productblMasterDataTable productblTable = getproductblMasterAllBybillNo(billNo);
                    if (productblTable.Rows.Count == 0)
                    {
                        productblMaster.productblMasterInsert(row);
                    }
                    else
                    {
                        row.billNo = "B" + Date + "-" + basicSql.getOrderSerail("备料单号");
                        productblMaster.productblMasterInsert(row);
                    }

                    //更新单据编号的过程 
                    string orderSerial = basicSql.getOrderSerail("备料单号");
                    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "备料单号");
                }
                if (state == "编辑")
                {
                    productblMaster.productblMasterUpdate(row);
                }


                foreach (purchase.productblDetailRow detailRow in pTable)
                {
                    if (detailRow.RowState != DataRowState.Deleted)
                    {
                        productblDetailRow.billNo = billNo ;
                        productblDetailRow.createDate = DateTime .Now ;
                        productblDetailRow.createUser = user;
                        productblDetailRow.recordid = detailRow.recordid;
                        if (!detailRow.IsgsNull())
                        {
                            productblDetailRow.gs = detailRow.gs;
                        }
                        else { productblDetailRow.gs  = 0; }
                        if (!detailRow.IsoldgsNull ())
                        {
                            productblDetailRow.oldgs = detailRow.oldgs;
                        }
                        else { productblDetailRow.oldgs = 0; }
                        if (!detailRow.IsoldtsNull ())
                        {
                            productblDetailRow.oldts = detailRow.oldts;
                        }
                        else { productblDetailRow.oldts = 0; }
                        if (!detailRow.IswbNull ())
                        {
                            productblDetailRow.wb = detailRow.wb;
                        }
                        else { productblDetailRow.wb = ""; }
                        if (!detailRow.IsmatNameNull())
                        {
                            productblDetailRow.matName = detailRow.matName;
                        }
                        else { productblDetailRow.matName = ""; }
                        productblDetailRow.matNum = detailRow.matNum;
                        if (!detailRow.IspzNull())
                        {
                            productblDetailRow.pz = detailRow.pz;
                        }
                        else { productblDetailRow.pz=""; }
                        if (!detailRow.IsstockKgNull ())
                        {
                            productblDetailRow.stockKg = detailRow.stockKg;
                        }
                        else { productblDetailRow.stockKg = 0; }
                        if (!detailRow.IstsNull ())
                        {
                            productblDetailRow.ts = detailRow.ts;
                        }
                        else { productblDetailRow.ts = 0; }
                        if (!detailRow.IsstockqtyNull())
                        {
                            productblDetailRow.stockqty = detailRow.stockqty;
                        }
                        else { productblDetailRow.stockqty = 0; }
                        if (!detailRow.IsstockLengthNull ())
                        {
                            productblDetailRow.stockLength = detailRow.stockLength;
                        }
                        else { productblDetailRow.stockLength = 0; }
                        if (!detailRow.IspweightNull ())
                        {
                            productblDetailRow.pweight = detailRow.pweight;
                        }
                        else { productblDetailRow.pweight = 0; }
                        if (!detailRow.IsmarkNull ())
                        {
                            productblDetailRow.mark = detailRow.mark;
                        }
                        else { productblDetailRow.mark = ""; }

                    }

                    //编辑子表记录,新增
                    if (detailRow.RowState == DataRowState.Added)
                    {

                        productblDetail.productblDetailInsert(productblDetailRow);


                    }
                    //修改
                    if (detailRow.RowState == DataRowState.Modified)
                    {
                        productblDetail.productblDetailUpdate(productblDetailRow);
                    }
                    //删除
                    if (detailRow.RowState == DataRowState.Deleted)
                    {
                        DataRow delrow = (DataRow)detailRow;
                        productblDetail.productblDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                    }


                }

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个备料单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行领料退库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int productblMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productblMasterDAL productblMaster = new productblMasterDAL(tn, conn);
                productblMaster.productblMasterDel(billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }
    }
}
